home *** CD-ROM | disk | FTP | other *** search
/ 1,000+ Great Games / 1_1000 Games.iso / DOSGAMES / BOGGLE.ZIP / SOURCE.ZIP / SQUARE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-03  |  11.7 KB  |  341 lines

  1. /*****************************************************************************
  2. * Program:  SQUARE.CPP
  3. * Purpose:  This module handles the events associated with the squares on the 
  4. *           game board.
  5. *****************************************************************************/
  6. #include  "bogwin.hpp"
  7.  
  8. Boolean isButton1Down=false;  //keep this global for all squares
  9.                               // so that the mouse movement tracking
  10.                               // can tell if the button is down while
  11.                               // you are in "drag mode"
  12.  
  13.  
  14. /*****************************************************************************
  15. * Function: TBogSquare
  16. * Parms:    Pointers to objects it needs to communicate with as well as a
  17. *           row and column ID it can associate with this square
  18. * Purpose:  Instantiate the square and save the pointers to other objects
  19. * Returns:  Nothing
  20. *****************************************************************************/
  21. TBogSquare::TBogSquare (unsigned long id,
  22.                         IWindow* parent,
  23.                         char* letter, 
  24.                         TBogWindow* PBWindow,
  25.                         TBogBoard* PBBoard,
  26.                         LastSquare* PBLastSq,
  27.                         TBogWIP* PBWIP,
  28.                         TBogWordList* PBLIST,
  29.                         TBogScore* PBScore,
  30.                         Sound* pSound,
  31.                         int row, int col,
  32.                         const IRectangle& initial)
  33.       : IBitmapControl(id, parent, parent, ID_BLANK, initial),
  34.       myfont(this)
  35. {
  36.  
  37.    /****************************************************
  38.    * Each square needs to know what row and col it is.
  39.    * The for loop is indexed starting with 0 so we
  40.    * need to add 1 to it.
  41.    ****************************************************/
  42.    rowid = row+1;  colid = col+1;
  43.  
  44.    /***************************************************
  45.    * Save the address of the LastSquare in the square
  46.    * private data member.  We will use this
  47.    * later to send the square text to the WIP.
  48.    ***************************************************/
  49.    PBLastSquare = PBLastSq;
  50.  
  51.    /*********************************************
  52.    * Save the address of the WIP in the square
  53.    * private data member.  We will use this
  54.    * later to send the square text to the WIP.
  55.    *********************************************/
  56.    pbwip = PBWIP;
  57.    pbwindow = PBWindow;
  58.    pbboard = PBBoard;
  59.    pblist = PBLIST;
  60.    pbscore = PBScore;
  61.    psound = pSound;
  62.  
  63.    HiLite = false;
  64.    normalColor = new IColor(204,204,204);
  65.    selectedColor = IColor::white;
  66.  
  67.    setAlignment(IStaticText::centerCenter);
  68.    myfont.setName("Swiss")
  69.          .setPointSize(10)
  70.          .setBold()
  71.          .setItalic();
  72.   
  73.    setBackgroundColor(*normalColor);
  74.    setFont((myfont));
  75.  
  76.    strcpy(Letter, "");
  77.    setText(Letter);
  78.  
  79.    ((IMouseHandler*)this)->handleEventsFor(this);
  80.    ((IPaintHandler*)this)->handleEventsFor(this);
  81.    ((AMouseMoveHandler*)this)->handleEventsFor(this);
  82. }
  83.  
  84. /*****************************************************************************
  85. * Function: ~TBogSquare
  86. * Parms:    Destructor
  87. * Purpose:  delete any instantiated objects
  88. * Returns:  Nothing
  89. *****************************************************************************/
  90. TBogSquare::~TBogSquare()
  91. {
  92.    delete normalColor;
  93.    return;
  94. }
  95.  
  96. /*****************************************************************************
  97. * Function: mouseClicked
  98. * Parms:    event - info about which mouse button was pressed
  99. * Purpose:  Process the mouse messages generated from the square
  100. * Returns:  false - so someone else can process the message
  101. *****************************************************************************/
  102. Boolean TBogSquare::mouseClicked(IMouseClickEvent &event)
  103. {
  104.   if ( event.mouseButton() == IMouseClickEvent::button1 &&
  105.        event.mouseAction() == IMouseClickEvent::down )
  106.   {
  107.     button1Down();
  108.   }
  109.   else if ( event.mouseButton() == IMouseClickEvent::button1 &&
  110.             event.mouseAction() == IMouseClickEvent::up )
  111.   {
  112.     button1Up();
  113.   }
  114.   else if ( event.mouseButton() == IMouseClickEvent::button2 &&
  115.             event.mouseAction() == IMouseClickEvent::down )
  116.   {
  117.     button2Down();
  118.   }
  119.   return false;         // Allow someone else to also process.
  120. }
  121.  
  122.  
  123. /*****************************************************************************
  124. * Function: button1Down
  125. * Parms:    none
  126. * Purpose:  called when the left mouse button is clicked.  It will hilite the
  127. *           selected square and add the letter to the word in process edit
  128. *           field.  It also will record the letter selected for adjacency 
  129. *           testing later on.
  130. * Returns:  Nothing
  131. *****************************************************************************/
  132. void TBogSquare::button1Down()
  133. {
  134.    isButton1Down = true;
  135.  
  136.    if (!HiLite && pbwindow->isGameOn())  //game in play mode and not already hilighted
  137.    {
  138.       if (PBLastSquare->isValidSquare(rowid, colid))  //adjaced square??
  139.       {
  140.          HiLite = TRUE;
  141.          setBackgroundColor(selectedColor);
  142.  
  143.          refresh();  //force a repaint
  144.  
  145.          /*********************************************
  146.          *  Send Letter to WIP static field 
  147.          *********************************************/
  148.          pbwip->addLetter(Letter);
  149.          PBLastSquare->setSquare(rowid, colid); //keep track of this for adjacency
  150.       }                                         //  testing later on.
  151.       else
  152.       {
  153.          setBackgroundColor(*normalColor);
  154.       }
  155.    }
  156.    return;
  157. }
  158.  
  159. /*****************************************************************************
  160. * Function: button1Up
  161. * Parms:    none
  162. * Purpose:  called when the left mouse button let go.  This will validate 
  163. *           that you selected a valid square and will play a sound if not.
  164. * Returns:  Nothing
  165. *****************************************************************************/
  166. void TBogSquare::button1Up()
  167. {
  168.    if (!HiLite && pbwindow->isGameOn())
  169.    {
  170.        if (!PBLastSquare->isValidSquare(rowid, colid))
  171.        {
  172.           if(pbwindow->GetSoundFlag())
  173.              psound->playSound("badword.wav");     //Not a valid word
  174.        }
  175.    }
  176.  
  177.    isButton1Down = false;
  178.    return;
  179. }
  180.  
  181. /*****************************************************************************
  182. * Function: button2Down
  183. * Parms:    none
  184. * Purpose:  This will try to move the valid word from the word in process 
  185. *           list to the word list listbox.  It will also call the scoring 
  186. *           routines for the valid word.
  187. * Returns:  Nothing
  188. *****************************************************************************/
  189. void TBogSquare::button2Down()
  190. {
  191.    int irc;
  192.    char UString[WORDLENGTH];
  193.  
  194.    if (!pbwindow->isGameOn())  //if the game if over get out!
  195.        return;
  196.  
  197.    //convert it to upper case
  198.    pbwip->upString(UString, pbwip->getWIPString());
  199.  
  200.    /*********************************************
  201.    *  Send Word in Process to list box only if...
  202.    *       1) word is >= 3 letters in length
  203.    *       2) word is not a duplicate
  204.    *       3) word is a real English word
  205.    *********************************************/
  206.    if (strlen(UString) > 2)
  207.    {
  208.       char *prefix;
  209.       /********************************************************
  210.       *  Do not allow duplicate words...search the list box
  211.       ********************************************************/
  212.       irc = pblist->locateText(UString); 
  213.       if (irc >= 0)  //already got this one
  214.       {
  215.          prefix = UString;
  216.          pblist->deselectAll();                           //clear prev error
  217.          pblist->selectWord(irc);                         //show their error
  218.  
  219.          // Let them know they goofed!
  220.          if(pbwindow->GetSoundFlag())
  221.            psound->playSound("usedword.wav");
  222.       }      
  223.       /*********************************************
  224.       *  And do not allow non-English words
  225.       *********************************************/
  226.       else
  227.          if (pbwip->checkWord(UString) < 0)
  228.          {
  229.            // Let them know they goofed!
  230.            if(pbwindow->GetSoundFlag())
  231.                psound->playSound("boing.wav");
  232.          }
  233.       else
  234.       {  //must be a valid word
  235.          pblist->addString(UString);
  236.          pbscore->calcScore(strlen(UString));
  237.          pbscore->displayScore();
  238.          pblist->deselectAll();                            //take off hilite
  239.  
  240.          if(pbwindow->GetSoundFlag())
  241.          {
  242.             if(strlen(UString) > 4)
  243.                 psound->playSound("excelnt.wav");      //Excellent Word!!
  244.              else
  245.                 psound->playSound("goodword.wav");     //Got a good word anyway
  246.          }
  247.       }
  248.    }
  249.    else
  250.    {
  251.       //Must be less than two letters!!
  252.       if(pbwindow->GetSoundFlag())
  253.          psound->playSound("badword.wav");             //Not a valid word
  254.    }
  255.    pbwip->resetString();                           //Clear out WIP
  256.    clearBoard();                                   //Clear entries on board
  257.    return;
  258. }
  259.  
  260. /*****************************************************************************
  261. * Function: setSquareText
  262. * Parms:    SquareLetter - the letter to display in the box
  263. * Purpose:  This will set up the game so the paint function will display the
  264. *           desired letter.
  265. * Returns:  Nothing
  266. *****************************************************************************/
  267. void TBogSquare::setSquareText(char *SquareLetter)
  268. {
  269.    strcpy(Letter, SquareLetter);
  270.    refresh();
  271. }
  272.  
  273.  
  274. /*****************************************************************************
  275. * Function: paintWindow
  276. * Parms:    event
  277. * Purpose:  show the letter in the box
  278. * Returns:  false - let someone else process the message
  279. *****************************************************************************/
  280. Boolean TBogSquare::paintWindow(IPaintEvent &event)
  281. {
  282.    setText(Letter);
  283.    return false;
  284. }
  285.  
  286.  
  287. /*****************************************************************************
  288. * Function: clearBoard
  289. * Parms:    none
  290. * Purpose:  tell the board to clear ALL the squares and reset 
  291. *           things back to an initial state
  292. * Returns:  Nothing
  293. *****************************************************************************/
  294. void TBogSquare::clearBoard()
  295. {
  296.    pbboard->ClearBoard ();
  297.    return;
  298. }
  299.  
  300. /*****************************************************************************
  301. * Function: reset
  302. * Parms:    none
  303. * Purpose:  fix the color of the square
  304. * Returns:  Nothing
  305. *****************************************************************************/
  306. void TBogSquare::reset ()
  307.  
  308. {
  309.    setBackgroundColor(*normalColor);
  310.    if (HiLite)
  311.       HiLite = FALSE;
  312. }
  313.  
  314. /*****************************************************************************
  315. * Function: motion
  316. * Parms:    evt
  317. * Purpose:  for the square to be selected during drag mode,  we want the 
  318. *           mouse to be positioned more towards the center of the square.
  319. * Returns:  false - allow standard processing
  320. *****************************************************************************/
  321. Boolean TBogSquare::motion( IEvent &evt )
  322. {
  323.    unsigned short xCoordinate = evt.parameter1().lowNumber();
  324.    unsigned short yCoordinate = evt.parameter1().highNumber();
  325.  
  326.    /************************************************
  327.    * This ensures that you have to be somewhere near
  328.    * the center of the square to actually select it.
  329.    * It makes it far easier to use the mouse in drag
  330.    * mode this way
  331.    ************************************************/
  332.    if(isButton1Down)
  333.       if (  (xCoordinate > 10 && xCoordinate < 30) && 
  334.             (yCoordinate > 10 && yCoordinate < 30) )
  335.          button1Down();
  336.  
  337.   return false;  // allow standard processing
  338. }  
  339.  
  340.  
  341.